from database.db_handler import MysqlHander

class CustomTaskData:
    def __init__(self):
        self.db = MysqlHander("config.ini")
        pass

    def update_task(self, id, status, url):
        update = "update db_custom_task set Fstatus=" + str(status) + ",Furl='" + url + "',Ffinish_time=now(),Fmodify_time=now() where Fid = " + str(id) + ";"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def update_task_status(self, id, status):
        update = "update db_custom_task set Fstatus=" + str(status) + ",Fmodify_time=now() where Fid = " + str(id) + ";"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def query_task(self):
        query = "select Fid,Ftask_date,Fentity_name,Fstart_date,Fcustom_word,Femail from db_custom_task where Fstatus=0 order by Ftask_date asc limit 5;"
        print(query)
        return self.db.query_sql(query)

    def query_task_for_write(self):
        query = "select Fid,Ftask_date,Fentity_name,Fstart_date,Fcustom_word,Furl,Fstatus from db_custom_task where Fstatus in (5,2,3,4) limit 300;"
        print(query)
        return self.db.query_sql(query)

    def query_task_all(self):
        query = "select Fid,Fentity_name,Freject_word from db_custom_task where Fstatus in (2,3,4);"
        print(query)
        return self.db.query_sql(query)

    def query_old_task(self, old_date):
        query = "select Fid,Ftask_date from db_custom_task where Ftask_date<'" + old_date + "' and Fstatus in (2,3,4) order by Ftask_date asc limit 5;"
        print(query)
        return self.db.query_sql(query)

    def update_task_detail_status(self, id):
        update = "update db_custom_task_detail set Fstatus=1,Fmodify_time=now() where Fid = " + str(id) + ";"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass


    def update_old_task(self, id, old_date, current_date):
        update = "update db_custom_task set Fstatus=0,Furl='',Fmodify_time=now(),Ftask_date='" + current_date + "' where Fid = " + str(id) + " and Ftask_date='" + old_date + "';"
        
        print(update)
        try:
            #pass
            self.db.execute_not_safe(update)
        except Exception as e:
            print(str(e))
            pass

    def query_task_for_email(self):
        query = "select Fid,Ftask_date,Fentity_name,Fstart_date,Fcustom_word,Furl,Femail,Fcreate_time,Ffinish_time from db_custom_task where Fstatus=2 limit 10;"
        print(query)
        return self.db.query_sql(query)

    def query_email_task_by_date(self, task_date):
        query = "select Fid,Ftask_date,Fentity_name,Fstart_date,Fcustom_word,Furl,Femail from db_custom_task where Ftask_date ='" + task_date + "' and Fstatus in (3,4);"
        print(query)
        return self.db.query_sql(query)

    def query_email_class(self, limit_date):
        query = "select Fclass from db_custom_task where Ftask_date >='" + limit_date + "' and Fstatus in (3,4) group by(Fclass);"
        print(query)
        return self.db.query_sql(query)

    def query_email_task_by_class(self, class_name, limit_date):
        query = "select Fid,Ftask_date,Fentity_name,Fstart_date,Fcustom_word,Furl,Femail from db_custom_task where Ftask_date >='" + limit_date + "' and Fclass =" + str(class_name) + " and Fstatus in (2,3,4) order by Fentity_name desc;"
        print(query)
        return self.db.query_sql(query)        

    def query_entity_info(self, entity_name):
        query = "select Fpre_attr,Fpost_attr,Fentity_set from db_entity_set_data where Fname='" + entity_name + "' limit 1;"
        return self.db.query_sql(query)

    def query_user_entity_info(self, entity_name, user):
        query = "select '','',Fentity_set from db_entity_suggest where Fname='" + entity_name + "' and Fuser='" + user + "' limit 1;"
        return self.db.query_sql(query)

    def add_task_detail_data(self, task_id, data_date, data_title, data_url, data_site):
        insert = "insert into db_custom_task_detail (Ftask_id,Fdata_site,Fdata_title,Fdata_date,Fdata_url,Fstatus,Fcreate_time,Fmodify_time)"
        insert += " values (" + str(task_id) + ", '" + data_site + "','" + data_title + "','" + data_date + "','" + data_url + "',"
        insert += "0, now(), now());"
        
        print(insert)
        try:
            #pass
            self.db.execute_not_safe(insert)
        except Exception as e:
            print(str(e))
            pass

    def query_task_detail_data(self, task_id):
        query = "select Fdata_site,Fdata_title,Fdata_date,Fdata_url from db_custom_task_detail where Ftask_id=" + str(task_id) + " and Fstatus=0 order by Fdata_date desc;"
        return self.db.query_sql(query)

    def query_task_detail_all(self, task_id):
        query = "select Fid,Ftask_id,Fdata_site,Fdata_title,Fdata_date,Fdata_url,Fstatus,Fcreate_time,Fmodify_time from db_custom_task_detail where Ftask_id=" + str(task_id) + " and Fstatus=0;"
        return self.db.query_sql(query)

    def query_task_max_date(self, task_id):
        query = "select Fdata_date from db_custom_task_detail where Ftask_id=" + str(task_id) + " order by Fdata_date desc limit 1;"
        return self.db.query_sql(query)